import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-01-10
 * Time: 下午 21:19
 */

@WebServlet("/login")
public class loginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取请求中的信息来判断是否是正确的用户信息
        resp.setContentType("text/html;charset=utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if(username == null || username.equals("") || password == null || password.equals("")) {
            resp.getWriter().write("填写信息不完整,请从新输入!");
            return;
        }

        if(!username.equals("zhangsan") || !password.equals("123")) {
            resp.getWriter().write("用户名或者密码错误,请从新输入");
            return;
        }

        HttpSession session = req.getSession(true);
        session.setAttribute("username","zhangsan");
        resp.sendRedirect("index");
    }
}
